package com.shiliao.app.activity.Feedback.ui;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.shiliao.app.R;
import com.shiliao.app.activity.Feedback.presenter.DynamicPresenter;
import com.shiliao.app.activity.Feedback.view.DynamicView;
import com.shiliao.app.activity.MainActivity;
import com.shiliao.app.activity.index.ui.NewsDetailActivity;
import com.shiliao.app.manager.YMUserManager;
import com.shiliao.app.utils.MyWebView;
import com.shiliao.app.utils.WebSetUtil;
import com.shiliao.app.utils.YMServerUtil;
import com.shiliao.app.weight.ActionBarView;
import com.shiliao.app.weight.ProgressWebView;
import com.yimei.framework.base.ui.BaseFragment;
import com.yimei.framework.util.LogUtil;

import java.security.PrivateKey;

import butterknife.BindView;

/**
 * Created by yinzhiqiang on 2017/9/6.
 */

public class DynamicFragment extends BaseFragment implements DynamicView,
        MainActivity.IPageChangedListener {

    @BindView(R.id.title_bar)
    ActionBarView mActionBar;

    @BindView(R.id.webview)
    ProgressWebView webview;

    @BindView(R.id.swip_refresh)
    SwipeRefreshLayout refresh;
    private MyWebView mWebView;

    private DynamicPresenter dynamicPresenter;
    @Override
    public int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    public void onPageChanged(int position) {
        if (MainActivity.getCurrentView() == MainActivity.POSITON_FEEDBACK) {
            if (getView() == null && getActivity() != null) {
                mContext = getActivity();
                initFragment();
            } else if (!isInit
                    && getView() != null) {
                initFragment();
            } else if (isInit && getView() != null) {
                //     requestData();
            }
        }
    }


    private void initFragment() {
        isInit = true;
        LogUtil.e("yjl", "===IndexFragment===init");
        mActionBar.setTitles("社区动态");
//        dynamicPresenter=new DynamicPresenter(this);
//        dynamicPresenter.requestDynamic();
        final String url = YMServerUtil.getServerUrl() + YMServerUtil.ACTION_DYNMIC + "?user_id=" + YMUserManager.getUserId(getContext());
        WebSetUtil.setWebView(webview);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webview.reload();
            }
        });
        webview.loadUrl(url);
        WebRef webRef=new WebRef();
       webview.setWebViewClient(webRef);
    }

    class WebRef extends WebViewClient{
        public WebRef() {
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (refresh != null) {
                refresh.setRefreshing(false);
            }
        }
    }

}
